#蔡徐坤
from flask import Flask, jsonify, request, render_template

students = [
    {'id': 1, 'name': '张三', 'gender': '男', 'age': 18,
     'id_card': '123456789', 'phone': '12345678901', 'address': '北京'},
    {'id': 2, 'name': '李四', 'gender': '男', 'age': 19,
     'id_card': '123456780', 'phone': '12345678902', 'address': '上海'},
    {'id': 3, 'name': '王五', 'gender': '女', 'age': 20,
     'id_card': '123456781', 'phone': '12345678903', 'address': '广州'}
]

courses = [
    {'id':1,'name':'python程序设计','teacher':'张良峰','age':64,'week':'星期三','address':'4-411'}
    ]


app = Flask(__name__)

@app.route('/') # 获取学生信息页面
def index():
    return render_template('index.html')

@app.route('/cos') # 获取课程信息页面
def cos():
    return render_template('course.html')

@app.route('/nav')
def nav():
    return render_template('nav.html')

# 列表展示学生信息
@app.route('/students', methods=['GET'])
def get_students():
    return jsonify(students)

# 列表展示课程信息
@app.route('/courses', methods=['GET'])
def get_courses():
    return jsonify(courses)

# 添加学生信息
@app.route('/coursess', methods=['POST'])
def add_courses():
    new_courses = {
        'id': int(request.json['id']),
        'name': request.json['name'],
        'age': request.json['age'],
        'week': request.json['week'],
        'teacher': request.json['teacher'],
        'address': request.json['address']
    }
    students.append(new_courses)
    return jsonify(new_courses)

# 获取单个学生信息
@app.route('/students/<int:id>', methods=['GET'])
def get_student(id):
    for student in students:
        if student['id'] == id:
            return jsonify(student)
    return jsonify({'error': 'Student not found'})

# 修改学生信息
@app.route('/students/<int:id>', methods=['PUT'])
def update_student(id):
    for student in students:
        if student['id'] == id:
            student['name'] = request.json['name']
            student['gender'] = request.json['gender']
            student['age'] = request.json['age']
            student['id_card'] = request.json['id_card']
            student['phone'] = request.json['phone']
            student['address'] = request.json['address']
            return jsonify(student)
    return jsonify({'error': 'Student not found'})

# 删除学生信息
@app.route('/students/<int:id>', methods=['DELETE'])
def delete_student(id):
    for student in students:
        if student['id'] == id:
            students.remove(student)
            return jsonify({'result': True})
    return jsonify({'error': 'Student not found'})

if __name__ == '__main__':
    app.run(debug=True,host='0.0.0.0',port=80)
